package com.example.fengsam.aidong.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.fengsam.aidong.R;
import com.example.fengsam.aidong.adapter.HistroyStepAdapter;
import com.example.fengsam.aidong.bean.StepData;
import com.example.fengsam.aidong.utils.DbUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HistroyStepActivity extends BaseActivity {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.layout_titlebar)
    LinearLayout layoutTitlebar;
    @BindView(R.id.rv_histroy_step_list)
    RecyclerView rvHistroyStepList;

    HistroyStepAdapter mAdapter;

    List<StepData> stepDatas;

    private View notDataView;
    private View errorView;
    private boolean mError = false ;
    private boolean mNoData = false;
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_histroy_step);
//        ButterKnife.bind(this);
//        initView();
//    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_histroy_step);
        ButterKnife.bind(this);
        initView();
    }

    private void initView()
    {
        rvHistroyStepList.setHasFixedSize(true);
        rvHistroyStepList.setLayoutManager(new LinearLayoutManager(this));
        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) rvHistroyStepList.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvHistroyStepList.getParent(), false);
        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });
        mNoData=initData();
        initAdapter();
        onRefresh();

    }

    public void initAdapter()
    {
        mAdapter=new HistroyStepAdapter(R.layout.histroy_step_item,stepDatas);
        rvHistroyStepList.setAdapter(mAdapter);
    }

    public boolean initData()
    {
        if (DbUtils.getQueryAll(StepData.class)==null)
            return true;
        else
        stepDatas = DbUtils.getQueryAll(StepData.class);
        return false;
    }

    @OnClick({R.id.iv_left})
    public void OnClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_left:
                finish();
                break;
        }
    }

    private void onRefresh() {
        //点击暂无数据后重新加载
        mNoData=initData();
        mAdapter.notifyDataSetChanged();
        mAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvHistroyStepList.getParent());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mError) {
                    mAdapter.setEmptyView(errorView);
                    mError = false;
                } else {
                    if (mNoData) {
                        mAdapter.setEmptyView(notDataView);
                        mNoData = false;
                    } else {
                        initAdapter();
                    }
                }
            }
        }, 1000);
    }


}
